chore: improve mock asset controller data source tests#7958
chore: improve mock asset controller data source tests#7958Prithpal-Sooriya merged 3 commits intomainfrom
Conversation
This commit introduces a new mock implementation of the AssetControllerMessenger, which facilitates testing by simulating various actions and events related to asset management. The mock includes methods for creating a mock provider, registering action handlers, and simulating network states. Additionally, it updates the RpcDataSource and StakedBalanceDataSource tests to utilize the new mock, improving test reliability and coverage. Refactors existing tests to remove unnecessary complexity and improve clarity, ensuring that the mock accurately reflects the expected behavior of the real messenger in a controlled environment.
|
|
||
| // Test escape hatch for mocking areas that do not need explicit types | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| type TestMockType = any; |
There was a problem hiding this comment.
Only 1 or 2 places are using the escape hatch 🤞🏾
| rootMessenger.delegate({ | ||
| messenger: assetsControllerMessenger, | ||
| actions: [ | ||
| // AssetsController | ||
| 'AccountTreeController:getAccountsFromSelectedAccountGroup', | ||
| // RpcDataSource | ||
| 'TokenListController:getState', | ||
| 'NetworkController:getState', | ||
| 'NetworkController:getNetworkClientById', | ||
| // RpcDataSource, StakedBalanceDataSource | ||
| 'NetworkEnablementController:getState', | ||
| // SnapDataSource | ||
| 'SnapController:getRunnableSnaps', | ||
| 'SnapController:handleRequest', | ||
| 'PermissionController:getPermissions', | ||
| // BackendWebsocketDataSource | ||
| 'BackendWebSocketService:connect', | ||
| 'BackendWebSocketService:disconnect', | ||
| 'BackendWebSocketService:forceReconnection', | ||
| 'BackendWebSocketService:sendMessage', | ||
| 'BackendWebSocketService:sendRequest', | ||
| 'BackendWebSocketService:getConnectionInfo', | ||
| 'BackendWebSocketService:getSubscriptionsByChannel', | ||
| 'BackendWebSocketService:channelHasSubscription', | ||
| 'BackendWebSocketService:findSubscriptionsByChannelPrefix', | ||
| 'BackendWebSocketService:addChannelCallback', | ||
| 'BackendWebSocketService:removeChannelCallback', | ||
| 'BackendWebSocketService:getChannelCallbacks', | ||
| 'BackendWebSocketService:subscribe', | ||
| ], | ||
| events: [ | ||
| // AssetsController | ||
| 'AccountTreeController:selectedAccountGroupChange', | ||
| 'KeyringController:lock', | ||
| 'KeyringController:unlock', | ||
| 'PreferencesController:stateChange', | ||
| // RpcDataSource, StakedBalanceDataSource | ||
| 'NetworkController:stateChange', | ||
| 'TransactionController:transactionConfirmed', | ||
| 'TransactionController:incomingTransactionsReceived', | ||
| // StakedBalanceDataSource | ||
| 'NetworkEnablementController:stateChange', | ||
| // SnapDataSource | ||
| 'AccountsController:accountBalancesUpdated', | ||
| 'PermissionController:stateChange', | ||
| // BackendWebsocketDataSource | ||
| 'BackendWebSocketService:connectionStateChanged', | ||
| ], | ||
| }); |
There was a problem hiding this comment.
Yeah lets def sync on the AssetController messenger.
This messenger has way too many responsibilities.
Ideally each data source should have its own responsibilities.
| 'BackendWebSocketService:connect', | ||
| 'BackendWebSocketService:disconnect', | ||
| 'BackendWebSocketService:forceReconnection', | ||
| 'BackendWebSocketService:sendMessage', | ||
| 'BackendWebSocketService:sendRequest', | ||
| 'BackendWebSocketService:getConnectionInfo', | ||
| 'BackendWebSocketService:getSubscriptionsByChannel', | ||
| 'BackendWebSocketService:channelHasSubscription', | ||
| 'BackendWebSocketService:findSubscriptionsByChannelPrefix', | ||
| 'BackendWebSocketService:addChannelCallback', | ||
| 'BackendWebSocketService:removeChannelCallback', | ||
| 'BackendWebSocketService:getChannelCallbacks', | ||
| 'BackendWebSocketService:subscribe', |
There was a problem hiding this comment.
Does the asset controller really need ALL the backend actions?
packages/assets-controller/src/data-sources/StakedBalanceDataSource.ts
Outdated
Show resolved
Hide resolved
…DataSource This commit cleans up the `destroy` method in the `StakedBalanceDataSource` class by removing the unsubscription logic for transaction and network events, which is no longer necessary. Corresponding test cases have also been removed to reflect this change.
| this.#handleStakedBalanceUpdate.bind(this), | ||
| ); | ||
|
|
||
| const unsubConfirmed = this.#messenger.subscribe( |
There was a problem hiding this comment.
.subscribe does not return a callback, so this was not needed.
| this.#unsubscribeTransactionConfirmed?.(); | ||
| this.#unsubscribeIncomingTransactions?.(); | ||
| this.#unsubscribeNetworkStateChange?.(); | ||
| this.#unsubscribeNetworkEnablementControllerStateChange?.(); |
There was a problem hiding this comment.
There is no callback to unsubscribe from.
| }); | ||
| }); | ||
|
|
||
| describe('destroy', () => { |
There was a problem hiding this comment.
Test is irrelevant. The controller destruction will clear messenger subscriptions.
BUT we share this messenger with other data sources, so a data source destruction might impact other data sources 💀
We can discuss delegating and generating diff messengers per data source.
packages/assets-controller/src/data-sources/StakedBalanceDataSource.test.ts
Show resolved
Hide resolved
salimtb
left a comment
There was a problem hiding this comment.
tested and work as expected
## Explanation This commit introduces a new mock implementation of the AssetControllerMessenger, which facilitates testing by simulating various actions and events related to asset management. The mock includes methods for creating a mock provider, registering action handlers, and simulating network states. Additionally, it updates the RpcDataSource and StakedBalanceDataSource tests to utilize the new mock, improving test reliability and coverage. Refactors existing tests to remove unnecessary complexity and improve clarity, ensuring that the mock accurately reflects the expected behavior of the real messenger in a controlled environment. ## References <!-- Are there any issues that this pull request is tied to? Are there other links that reviewers should consult to understand these changes better? Are there client or consumer pull requests to adopt any breaking changes? For example: * Fixes #12345 * Related to #67890 --> ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've communicated my changes to consumers by [updating changelogs for packages I've changed](https://github.com/MetaMask/core/tree/main/docs/processes/updating-changelogs.md) - [x] I've introduced [breaking changes](https://github.com/MetaMask/core/tree/main/docs/processes/breaking-changes.md) in this PR and have prepared draft pull requests for clients and consumer packages to resolve them <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Medium Risk** > Mostly test-only refactors, but it changes `StakedBalanceDataSource` subscription teardown behavior to rely on messenger-level cleanup, which could affect consumers with custom messenger implementations. > > **Overview** > Improves `assets-controller` data source tests by introducing a shared `MockAssetControllerMessenger` fixture that delegates the relevant actions/events and provides helpers for registering RPC/staking handlers and mocking a `Web3Provider`. > > Updates `RpcDataSource` and `StakedBalanceDataSource` tests to use the shared fixture, simplify network-state/event publishing via the root messenger, and tighten event-driven staking refresh assertions. Also exports `STAKING_INTERFACE` for reuse in mocks and makes small type-safety cleanups in `RpcDataSource` around state/token-list access and native asset ID construction. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit b2c402c. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
Explanation
This commit introduces a new mock implementation of the AssetControllerMessenger, which facilitates testing by simulating various actions and events related to asset management. The mock includes methods for creating a mock provider, registering action handlers, and simulating network states. Additionally, it updates the RpcDataSource and StakedBalanceDataSource tests to utilize the new mock, improving test reliability and coverage.
Refactors existing tests to remove unnecessary complexity and improve clarity, ensuring that the mock accurately reflects the expected behavior of the real messenger in a controlled environment.
References
Checklist
Note
Medium Risk
Mostly test-only refactors, but it changes
StakedBalanceDataSourcesubscription teardown behavior to rely on messenger-level cleanup, which could affect consumers with custom messenger implementations.Overview
Improves
assets-controllerdata source tests by introducing a sharedMockAssetControllerMessengerfixture that delegates the relevant actions/events and provides helpers for registering RPC/staking handlers and mocking aWeb3Provider.Updates
RpcDataSourceandStakedBalanceDataSourcetests to use the shared fixture, simplify network-state/event publishing via the root messenger, and tighten event-driven staking refresh assertions. Also exportsSTAKING_INTERFACEfor reuse in mocks and makes small type-safety cleanups inRpcDataSourcearound state/token-list access and native asset ID construction.Written by Cursor Bugbot for commit b2c402c. This will update automatically on new commits. Configure here.